home *** CD-ROM | disk | FTP | other *** search
/ Directorty Opus 5 - Magellan 2 / Opus 5 - Magellan 2.iso / Extras / DOpusLhARexx2 / ARexx / AddFilesLhA.rexx next >
OS/2 REXX Batch file  |  1994-07-01  |  9KB  |  426 lines

  1. /*rx
  2.  *
  3.  * AddFilesLhA.rexx - Add the selected files to an LhA archive previously
  4.  *                    listed in a DOpus window by ListLha.rexx.
  5.  *
  6.  * $VER: AddFilesLhA 40.7 (23/05/94) by Geoff Seeley
  7.  *
  8.  * Usage: ARexx command AddFilesLhA.rexx (from DOpus)
  9.  *
  10.  */
  11.  
  12. /* configuration variables (change these to suit your setup) */
  13.  
  14. LhaCommand   = 'XFH_Work:C/Archivers/File/LhA '
  15. OutputWindow = '>CON:30/145/640/100/LhA_Output/CLOSE/SCREENDOPUS.1 '
  16. InputWindow  = '<CON:30/245/640/50/LhA_Input/AUTO/SCREENDOPUS.1 '
  17. AddList      = 'T:lha_file_list'
  18. ListLhARexx  = "RX DOpus:ARexx/ListLhA.rexx"
  19.  
  20. /*---------------------------------------------------------------------------*/
  21.  
  22. /* misc. variables */
  23.  
  24. DOpusPort = 'DOPUS.1'
  25.  
  26. LhaAddCmd    = ''
  27. LhaAddBase   = '-m a '
  28. LhaRecurse   = '-r '
  29. LhaSavePath  = '-x '
  30. CreateNew    = 0
  31. AddSubDirs   = 0
  32.  
  33. if ~show(l,"rexxsupport.library") then        
  34.     call addlib("rexxsupport.library",0,-30,0)
  35.  
  36. /* make sure we've got somebody to talk to */
  37.  
  38. if showlist('Ports', DOpusPort) = 0 then do
  39.    say 'Directory Opus ARexx port not found. Aborting.'
  40.    call CleanUp
  41. end
  42.  
  43. /* make sure the right task is listening... */
  44.  
  45. address 'DOPUS.1'
  46. options results
  47.  
  48. TopText "Adding File(s) to an LhA Archive"
  49. Busy on
  50.  
  51. /* get the path/name of the LhA archive file */
  52.  
  53. 'Status 14 -1'
  54. LhaArchive = result
  55.  
  56. /* if this isn't an LhA buffer, try the other window then ask for new arc. */
  57.  
  58. if (IsLhAFile(LhaArchive) = 0) then do
  59.  
  60.    /* Hmm, let's try the other window */
  61.  
  62.    OtherWindow
  63.    'Status 14 -1'
  64.    LhaArchive = result
  65.  
  66.    /* if neither window looks right, look for selected file */
  67.  
  68.    if (IsLhAFile(LhaArchive) = 0) then do
  69.  
  70.       'GetNextSelected -1'
  71.       LhaArchive = RESULT
  72.       if LhaArchive ~= 0 & IsLhaFile(LhaArchive) then do
  73.          call SetYesNo
  74.          Request "Add files to selected archive? ("||LhaArchive||")"
  75.          UseArchive = RESULT
  76.          Busy on
  77.          call RestoreRequester
  78.          if UseArchive = 1 then do
  79.             'Status 13 -1'
  80.             LhaPath = Result
  81.             LhaArchive = LhaPath || LhaArchive
  82.             address COMMAND ListLhARexx
  83.          end
  84.          else do
  85.             OtherWindow
  86.             TopText "Adding files to LhA archive aborted."
  87.             call CleanUp
  88.          end
  89.       end
  90.       else do
  91.          call SetYesNo
  92.          Request "No LhA archive listed. Do you wish to create one?"
  93.  
  94.          if Result = 0 then do
  95.             OtherWindow
  96.             TopText "Adding files to LhA archive aborted."
  97.             call CleanUp
  98.          end
  99.          else
  100.             CreateNew = 1
  101.  
  102.          call RestoreRequester
  103.  
  104.          /* get path of window */
  105.  
  106.          'Status 13 -1'
  107.          LhaPath = Result
  108.  
  109.          /* get new archive */
  110.  
  111.          GetString "'Enter LhA Archive Name: (Path: '"||LhaPath||"')'"
  112.          LhaArchive = Result
  113.  
  114.          /* cancel it? */
  115.  
  116.          if RC = 1 then do
  117.             TopText "LhA archive creation aborted."
  118.             call CleanUp
  119.          end
  120.  
  121.          if (IsLhAFile(LhaArchive) = 0) then
  122.             LhaArchive = LhaArchive || ".lha"
  123.  
  124.          NewLhaArchive = LhaArchive
  125.          LhaArchive = LhaPath || LhaArchive
  126.       end
  127.    end
  128.    else do
  129.  
  130.       /* get path to archive */
  131.  
  132.       call FindLhAPath
  133.       LhaArchive = LhaPath || LhaArchive
  134.  
  135.    end
  136. end
  137. else do
  138.  
  139.    /* make sure we don't have *two* LhA buffers */
  140.  
  141.    OtherWindow
  142.    'Status 14 -1'
  143.  
  144.    if (IsLhAFile(result) = 1) then do
  145.  
  146.       Notify "Sorry, You have two LhA archives listed.\Cannot add files from LhA archive."
  147.       OtherWindow
  148.       call CleanUp
  149.  
  150.    end
  151.  
  152.    OtherWindow
  153.  
  154.    /* get path to archive */
  155.  
  156.    call FindLhAPath
  157.    LhaArchive = LhaPath || LhaArchive
  158.  
  159. end
  160.  
  161. /* check for existance of archive */
  162.  
  163. if CreateNew = 0 then do
  164.    if ~exists(LhaArchive) then do
  165.       Notify "Can't seem to find '" || LhaArchive || "'\Aborting."
  166.       call CleanUp
  167.    end
  168. end
  169.  
  170. /* switch to window with files to add... */
  171.  
  172. OtherWindow
  173.  
  174. /* get list of selected entries */
  175.  
  176. 'GetSelectedAll "|" -1'
  177. SelectedEntries = result
  178.  
  179. /* must select *something* man... */
  180.  
  181. if SelectedEntries = 'RESULT' then do
  182.  
  183.    Notify "Please select file(s) to add first..."
  184.    call CleanUp
  185.  
  186. end
  187.  
  188. NumberOfEntries = CountWords(SelectedEntries)
  189.  
  190. /* get source directory */
  191.  
  192. 'Status 13 -1'
  193. SrcDir = result
  194.  
  195. /* verify, (I think I'm paranoid :-) */
  196.  
  197. if SrcDir = 'RESULT' | SrcDir = "" then do
  198.    Notify "Unable to get source directory. Aborting."
  199.    call CleanUp
  200. end
  201.  
  202. /* ask user if they want the path included */
  203.  
  204. call SetYesNo
  205.  
  206. Request "Store pathname(s) of selected file(s) in LhA archive?"
  207. StorePath = Result
  208.  
  209. Request "Add any possible subdirectories?"
  210. AddSubDirs = Result
  211.  
  212. call RestoreRequester
  213.  
  214. /* form proper command */
  215.  
  216. LhaAddCmd = LhaAddBase
  217.  
  218. if StorePath = 1 then do
  219.    LhaAddCmd = LhaSavePath || LhaAddCmd
  220. end
  221. if AddSubDirs = 1 then do
  222.    LhaAddCmd = LhaRecurse || LhaAddCmd
  223. end
  224.  
  225. /* add the files */
  226.  
  227. call AddFileList
  228.  
  229. /* go back to the archive window */
  230.  
  231. OtherWindow
  232.  
  233. if CreateNew = 0 then do
  234.  
  235.    /* call ListLhA to re-list the LhA archive in the window */
  236.  
  237.    address command ListLhARexx
  238.  
  239. end
  240. else do
  241.  
  242.    /* rescan the directory */
  243.  
  244.    'Rescan -1'
  245.  
  246.    /* select the new archive and list it in the window */
  247.  
  248.    'Select '|| NewLhaArchive || ' onlyfiles name'
  249.    address command ListLhARexx
  250.  
  251. end
  252.  
  253. TopText "Finished adding selected file(s) to LhA archive."
  254.  
  255. call CleanUp
  256.  
  257. exit
  258.  
  259. /*---------------------------------------------------------------------------*/
  260.  
  261. AddFileList: /* build a list of selected files, add list */
  262.  
  263.    /* toast possible old list */
  264.  
  265.    if exists(AddList) then
  266.       delete(AddList)
  267.  
  268.    if ~open(FileList, AddList, 'W') then do
  269.       Notify "Can't open file " || AddList
  270.       call CleanUp
  271.    end
  272.  
  273.    TopText "Creating file(s) list..."
  274.  
  275.    do EntryNumber = 1 to NumberOfEntries
  276.       Entry = GetWord(EntryNumber, SelectedEntries)
  277.       File = Quote(Entry)
  278.       ScrollToShow Entry
  279.       call ReplaceMetaChars
  280.       writeln(FileList, File)
  281.    end
  282.  
  283.    close(FileList)
  284.  
  285.    TopText "Adding file(s) to LhA archive..."
  286.  
  287.    /* move into source directory */
  288.  
  289.    pragma(D, SrcDir)
  290.  
  291.    CliCommand = LhaCommand || OutputWindow || InputWindow || LhaAddCmd ||Quote(LhaArchive)
  292.    CliCommand = CliCommand || ' @' || AddList
  293.  
  294.    address command CliCommand
  295.  
  296. return
  297.  
  298. /*---------------------------------------------------------------------------*/
  299.  
  300. SetYesNo:  /* set requester strings */
  301.  
  302.    'Status 26 set Yes'
  303.    'Status 27 set No'
  304.  
  305. return
  306.  
  307. /*---------------------------------------------------------------------------*/
  308.  
  309. RestoreRequester: /* restore (default) requester strings */
  310.  
  311.    Busy On
  312.    'Status 26 set Okay'
  313.    'Status 27 set Cancel'
  314.  
  315. return
  316.  
  317. /*---------------------------------------------------------------------------*/
  318.  
  319. ReplaceMetaChars: /* replace special wildcards with ? */
  320.  
  321.    File = translate(File, '???', '()`', '?')
  322.  
  323. return
  324.  
  325. /*---------------------------------------------------------------------------*/
  326.  
  327. Quote: procedure /* add quotes to string */
  328.  
  329.    parse arg string
  330.  
  331. return '"'||string||'"'
  332.  
  333. /*---------------------------------------------------------------------------*/
  334.  
  335. StripQuotes: procedure /* strip quotes from string */
  336.  
  337.    parse arg string
  338.  
  339. return strip(string,, '"')
  340.  
  341. /*---------------------------------------------------------------------------*/
  342.  
  343. IsLhAFile: procedure   /* look at extension, return 1 if right, else 0 */
  344.  
  345.    parse arg AFileName
  346.  
  347.    lps = lastpos(".", AFileName)               
  348.    if lps = 0 then                           
  349.       return 0
  350.  
  351.    FileExt = upper(right(AFileName,length(AFileName)-lps))
  352.  
  353.    if FileExt ~= "LHA" & FileExt ~= "LZH" then
  354.       return 0
  355.    else
  356.       return 1
  357.  
  358. return 0
  359.  
  360. /*--------------------------------------------------------------------------*/
  361.  
  362. FindLhAPath: /* grab invisible file path to archive */
  363.  
  364.    /* find number of entries, path is the last one */
  365.  
  366.    'Status 6 -1'
  367.  
  368.    GetEntry Result
  369.    LhaPath = Result
  370.  
  371. return
  372.  
  373. /*--------------------------------------------------------------------------*/
  374.  
  375. GetWord: procedure /* get word from '|' separated string */
  376.  
  377.   parse arg number,words
  378.  
  379.   if(left(words,1) ~= '|') then
  380.      words = '|'||words
  381.  
  382.   do i=1 to number
  383.      idx = index(words, '|');
  384.      words = substr(words, idx+1)
  385.   end
  386.  
  387.   end = index(words, '|') - 1
  388.  
  389.   if words = "" then
  390.      return ""
  391.  
  392.   ret_str = substr(words, 1, end)
  393.  
  394. return ret_str
  395.  
  396. /*--------------------------------------------------------------------------*/
  397.  
  398. CountWords: procedure /* count words from '|' separated string */
  399.  
  400.    parse arg words
  401.  
  402.    count = 0
  403.    idx = index(words, '|')
  404.  
  405.    do while idx ~= 0
  406.      count = count + 1
  407.      words = substr(words, idx+1)
  408.      idx = index(words, '|')
  409.    end
  410.  
  411. return count
  412.  
  413. /*--------------------------------------------------------------------------*/
  414.  
  415. CleanUp: /* clean up files and exit */
  416.  
  417.    if exists(AddList) then
  418.       delete(AddList)
  419.  
  420.    Busy off
  421.  
  422.    exit
  423.  
  424. return
  425.  
  426.